//
//  UIView+Animation.m
//  ZhaoCaiMao
//
//  Created by xieyingze on 2017/3/13.
//  Copyright © 2017年 谢英泽. All rights reserved.
//

#import "UIView+Animation.h"

@implementation UIView (Animation)

//animation.repeatCount = HUGE_VALF;

#pragma mark - 跳动效果(固定频率)

- (void)setAnimationWithActive {
    POPSpringAnimation* springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewScaleXY];
    //它会先缩小到(0.5,0.5),然后再去放大到(1.0,1.0)
    springAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(1.0, 0.8)];
    springAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(1.0, 1.0)];
    springAnimation.springBounciness = 20;
    springAnimation.springSpeed = 0.25f;
    springAnimation.repeatCount = HUGE_VALF;
    //动画结束之后的回调方法
    [springAnimation setCompletionBlock:nil];
    [self pop_addAnimation:springAnimation forKey:@"SpringAnimation"];
}

#pragma mark - 心跳效果

- (void)setAnimationWithHeartBeat:(id)repeatTimes {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    animation.values =  @[@(1.0),@(1.1),@(1.0),@(0.9)];
    animation.keyTimes = @[@(0.0),@(0.5),@(0.8),@(1.0)];
    animation.calculationMode = kCAAnimationLinear;
    animation.duration = 0.5;
    animation.removedOnCompletion = YES;
    
    //当repeatTimes为999时为循环 animation.repeatCount = HUGE_VALF;
    if (repeatTimes == nil) {
        animation.repeatCount = 2;
    }else{
        if ([repeatTimes intValue] == 999) {
            animation.repeatCount = HUGE_VALF;
        }else{
            animation.repeatCount = [repeatTimes intValue];
        }
    }
    [self.layer addAnimation:animation forKey:@"SHOW"];
}

- (void)stopHeartBeat {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    animation.values =  @[@(1.0),@(1.0),@(1.0),@(1.0)];
    animation.keyTimes = @[@(0.0),@(0.5),@(0.8),@(1.0)];
    animation.calculationMode = kCAAnimationLinear;
    animation.duration = 0.25;
    animation.repeatCount = 1;
    animation.removedOnCompletion = YES;
    
    [self.layer addAnimation:animation forKey:@"SHOW"];
}

#pragma mark - 翻转效果

- (void)setAnimationWithOverTurnIn:(UIView *)superView from:(UIView *)firstView to:(UIView *)secondView {
    CGContextRef context=UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:superView cache:YES];
    
    //这里时查找视图里的子视图（这种情况查找，可能时因为父视图里面不只两个视图）
    NSInteger first= [[superView subviews] indexOfObject:firstView];
    NSInteger seconde= [[superView subviews] indexOfObject:secondView];
    
    //当父视图里面只有两个视图的时候，可以直接使用下面这段.
    //[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    
    [self exchangeSubviewAtIndex:first withSubviewAtIndex:seconde];
    
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
}

#pragma mark - 摇摆效果

- (void)setAnimationWithRock:(id)repeatTimes {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.values =  @[@(0),@(-M_PI_2/4),@(0),@(M_PI_2/4),@(0),@(-M_PI_2/8),@(0),@(M_PI_2/8),@(0)];
    animation.keyTimes = @[@(0.0),@(0.2),@(0.4),@(0.8),@(1.0),@(1.1),@(1.2),@(1.3),@(1.4)];
    animation.calculationMode = kCAAnimationLinear;
    animation.duration = 2.f;
    animation.removedOnCompletion = YES;
    
    //当repeatTimes为999时为循环 animation.repeatCount = HUGE_VALF;
    if (repeatTimes == nil) {
        animation.repeatCount = 2;
    }else{
        
        if ([repeatTimes intValue] == 999) {
            animation.repeatCount = HUGE_VALF;
        }else{
            animation.repeatCount = [repeatTimes intValue];
        }
    }
    [self.layer addAnimation:animation forKey:@"SHOW"];
}

#pragma mark - 旋转

- (void)setAnimationWithRotationView:(kRotateType)rotateType isRepeatTime:(id)repeatTime {
    CABasicAnimation* animation;
    
    if (rotateType == kRotateTypeX) {
        animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
    }else if (rotateType == kRotateTypeY){
        animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    }else{
        animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    }
    if (repeatTime == nil) {
        animation.repeatCount = 2;
    }else{
        if ([repeatTime integerValue] == 999) {
            animation.repeatCount = HUGE_VALF;
        }else{
            animation.repeatCount = [repeatTime integerValue];
        }
    }
    animation.toValue        = @(M_PI * 1);
    animation.duration       = 0.25f;
    animation.cumulative     = YES;
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    [self.layer addAnimation:animation forKey:@"3D"];
}

- (void)setAnimationWithRotationView:(kRotateType)rotateType isRepeatTime:(id)repeatTime duration:(CGFloat)duration {
    CABasicAnimation* animation;
    
    if (rotateType == kRotateTypeX) {
        animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
    }else if (rotateType == kRotateTypeY){
        animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    }else{
        animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    }
    
    //    animation.repeatCount = HUGE_VALF;
    if (repeatTime == nil) {
        animation.repeatCount = 2;
    }else{
        if ([repeatTime integerValue] == 999) {
            animation.repeatCount = HUGE_VALF;
        }else{
            animation.repeatCount = [repeatTime integerValue];
        }
    }
    
    animation.toValue        = @(M_PI * 1);
    animation.duration       = duration;
    animation.cumulative     = YES;
    animation.removedOnCompletion = YES;
    animation.fillMode = kCAFillModeForwards;
    [self.layer addAnimation:animation forKey:@"3D"];
}

#pragma mark - 震动效果

- (void)setAnimationWithShake {
    CGFloat t =2.0;
    
    CGAffineTransform leftQuake  =CGAffineTransformTranslate(CGAffineTransformIdentity, t,kUI_Zero);
    CGAffineTransform rightQuake =CGAffineTransformTranslate(CGAffineTransformIdentity,-t, kUI_Zero);
    
    // starting point
    self.transform = leftQuake;
    
    // important
    [UIView beginAnimations:@"earthquake" context:(__bridge void * _Nullable)(self)];
    [UIView setAnimationRepeatAutoreverses:YES];
    [UIView setAnimationRepeatCount:5];
    [UIView setAnimationDuration:0.05];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(earthquakeEnded:finished:context:)];
    
    // end here & auto-reverse
    self.transform = rightQuake;
    
    [UIView commitAnimations];
}

-(void)earthquakeEnded:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context {
    if([finished boolValue]) {
        UIView* item =(__bridge UIView*)context;
        item.transform =CGAffineTransformIdentity;
    }
}

#pragma mark - 数字转动

/**
 整数数字转动动画

 @param fromNumber 动画开始数字
 @param toNumber 动画结束数字
 */
- (void)setAnimationWithJumpIntNuberFormNumber:(NSInteger)fromNumber toNumber:(NSInteger)toNumber {
    POPBasicAnimation *anim = [POPBasicAnimation animation];
    //动画持续时间
    anim.duration = 1;
    //时间函数
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    //创建动画属性
    POPAnimatableProperty * prop = [POPAnimatableProperty propertyWithName:@"count" initializer:^(POPMutableAnimatableProperty *prop) {
        // 读的block函数
        prop.readBlock = ^(id obj, CGFloat values[]) {
            values[0] = [[obj description] floatValue];
        };
        // 写的block函数
        prop.writeBlock = ^(id obj, const CGFloat values[]) {
            [obj setText:[NSString stringWithFormat:@"%.0f",values[0]]];
        };
        // 力学阀值
        prop.threshold = 1;
    }];
    anim.property = prop;
    anim.fromValue = @(fromNumber);
    anim.toValue = @(toNumber);
    [self pop_addAnimation:anim forKey:@"counting"];
}
/**
 浮点数转动动画
 
 @param fromNumber 动画开始数字
 @param toNumber 动画结束数字
 */
- (void)setAnimationWithJumpNuberFormNumber:(CGFloat)fromNumber toNumber:(CGFloat)toNumber {
    POPBasicAnimation *anim = [POPBasicAnimation animation];
    //动画持续时间
    anim.duration = 2;
    //时间函数
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    //创建动画属性
    POPAnimatableProperty * prop = [POPAnimatableProperty propertyWithName:@"count" initializer:^(POPMutableAnimatableProperty *prop) {
        // 读的block函数
        prop.readBlock = ^(id obj, CGFloat values[]) {
            values[0] = [[obj description] floatValue];
        };
        // 写的block函数
        prop.writeBlock = ^(id obj, const CGFloat values[]) {
            [obj setText:[NSString stringWithFormat:@"%.2f",values[0]]];
        };
        // 力学阀值
        prop.threshold = 1;
    }];
    anim.property = prop;
    anim.fromValue = @(fromNumber);
    anim.toValue = @(toNumber);
    [self pop_addAnimation:anim forKey:@"counting"];
}

/**
 浮点数转动动画

 @param fromNumber 动画开始数字
 @param toNumber 动画结束数字
 @param complete 动画完成回调
 */
- (void)setAnimationWithJumpNuberFormNumber:(CGFloat)fromNumber toNumber:(CGFloat)toNumber complete:(kAnimationComplete)complete {
    POPBasicAnimation *anim = [POPBasicAnimation animation];
    //动画持续时间
    anim.duration = 2;
    //时间函数
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    //创建动画属性
    POPAnimatableProperty * prop = [POPAnimatableProperty propertyWithName:@"count" initializer:^(POPMutableAnimatableProperty *prop) {
        // 读的block函数
        prop.readBlock = ^(id obj, CGFloat values[]) {
            values[0] = [[obj description] floatValue];
        };
        // 写的block函数
        prop.writeBlock = ^(id obj, const CGFloat values[]) {
            [obj setText:[NSString stringWithFormat:@"%.2f",values[0]]];
        };
        // 力学阀值
        prop.threshold = 1;
    }];
    anim.property = prop;
    anim.fromValue = @(fromNumber);
    anim.toValue = @(toNumber);
    anim.completionBlock = complete;
    [self pop_addAnimation:anim forKey:@"counting"];
}

/**
 浮点数字转动动画

 @param fromNumber 动画开始数字
 @param toNumber 动画结束数字
 @param duration 动画时长
 @param complete 动画完成回调
 */
- (void)setAnimationWithJumpNuberFormNumber:(CGFloat)fromNumber toNumber:(CGFloat)toNumber duration:(CGFloat)duration complete:(kAnimationComplete)complete {
    POPBasicAnimation *anim = [POPBasicAnimation animation];
    //动画持续时间
    anim.duration = duration;
    //时间函数
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    //创建动画属性
    POPAnimatableProperty * prop = [POPAnimatableProperty propertyWithName:@"count" initializer:^(POPMutableAnimatableProperty *prop) {
        // 读的block函数
        prop.readBlock = ^(id obj, CGFloat values[]) {
            values[0] = [[obj description] floatValue];
        };
        // 写的block函数
        prop.writeBlock = ^(id obj, const CGFloat values[]) {
            [obj setText:[NSString stringWithFormat:@"%.2f",values[0]]];
        };
        // 力学阀值
        prop.threshold = 1;
    }];
    anim.property = prop;
    anim.fromValue = @(fromNumber);
    anim.toValue = @(toNumber);
    anim.completionBlock = complete;
    [self pop_addAnimation:anim forKey:@"counting"];
}

@end
